#include "rel.h"

char endOfFile = false;
FILE * fp;

int robMain()
{
  
  strcpy( fileName, "test.txt");
  
  file_open_write();
  file_write("this is a test", 14);
  file_close(1);
  // printf("buffer: %s\n", sendMessageBuffer);
  
  return 1;
}

int file_open_read()
{
	
  fp = fopen(fileName, "rb");
  
  if (fp == NULL)
    return -1;
  
  return 1;
}

int file_open_write()
{
  fp = fopen(fileName, "ab");


  if (fp == NULL)
    return -1;
  
  return 1;
}

int file_write(char *buffer, int bufsize)
{
  int fs;

  fs = fwrite(buffer, 1, bufsize, fp);    
  if(fs < 0)
    return -1;
  if(fs == 0)
    return 0;

  return fs;
}

int file_read()
{
  int fs, i;
  
    fs = fread(sendMessageBuffer+12, 1, DATA_LEN, fp);
 	
    
  if(fs <= 0)
    return -1;

  if (feof(fp) != 0){
  	if(debug)
  		puts("Reached the end of file. Setting eof to TRUE");
  	endOfFile = true;
  }
  else {
  	if(debug)
  	  puts("Still more to read.");
  	endOfFile = false;
	}
  return fs;
}

int file_close(char removefile)
{
  if (fclose(fp) != 0)
    return -1; //error in closing the file

  if (removefile == 1)
    if (remove(fileName) != 0)
      return -2; //error in removing file

  return 1;
}
